home *** CD-ROM | disk | FTP | other *** search
/ Aminet 43 / Aminet 43 (2001)(GTI - Schatztruhe)[!][Jun 2001].iso / Aminet / dev / moni / SystemViewer.lha / Source / SysResident.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-18  |  14.1 KB  |  622 lines

  1. /****h* SystemViewer/SysResident.c [1.0] ********************************
  2. *
  3. * NAME
  4. *    SysResident.c
  5. *
  6. * DESCRIPTION
  7. *    Show the user a GUI of the system-resident programs.
  8. *
  9. * NOTES
  10. *    GUI Designed by : Jim Steichen
  11. *************************************************************************
  12. *
  13. */
  14.  
  15. #include <string.h>
  16.  
  17. #include <exec/types.h>
  18. #include <exec/execbase.h>
  19. #include <exec/resident.h>
  20.  
  21. #include <intuition/intuition.h>
  22. #include <intuition/classes.h>
  23. #include <intuition/classusr.h>
  24. #include <intuition/gadgetclass.h>
  25.  
  26. #include <libraries/gadtools.h>
  27.  
  28. #include <graphics/displayinfo.h>
  29. #include <graphics/gfxbase.h>
  30.  
  31. #include <clib/exec_protos.h>
  32. #include <clib/intuition_protos.h>
  33. #include <clib/gadtools_protos.h>
  34. #include <clib/graphics_protos.h>
  35. #include <clib/utility_protos.h>
  36. #include <clib/diskfont_protos.h>
  37.  
  38. #include "CPGM:GlobalObjects/CommonFuncs.h"
  39.  
  40. #include "SysLists.h"
  41.  
  42. #define SLV       0
  43. #define Update    1
  44. #define AddBt     2
  45. #define RemoveBt  3
  46. #define ReplaceBt 4
  47. #define Cancel    5
  48.  
  49. #define SLVGADGET SRGadgets[ SLV ]
  50.  
  51. #define SR_CNT    6
  52.  
  53. IMPORT struct ExecBase *SysBase;
  54.  
  55. // -------------------------------------------------------------------
  56.  
  57. PRIVATE char vr[] = "$VER: SysResident 1.0 (15-Feb-2001) by J.T. Steichen";
  58.  
  59. // -------------------------------------------------------------------
  60.  
  61. PRIVATE struct TextFont     *SRFont  = NULL;
  62. PRIVATE struct Window       *SRWnd   = NULL;
  63. PRIVATE struct Gadget       *SRGList = NULL;
  64. PRIVATE struct IntuiMessage  SRMsg;
  65. PRIVATE struct Gadget       *SRGadgets[ SR_CNT ];
  66.  
  67. PRIVATE UWORD  SRLeft   = 0;
  68. PRIVATE UWORD  SRTop    = 16;
  69. PRIVATE UWORD  SRWidth  = 632;
  70. PRIVATE UWORD  SRHeight = 228;
  71. PRIVATE UBYTE *SRWdt    = (UBYTE *) "System Residents Info:";
  72.  
  73. PRIVATE UBYTE *ttl = "Address  Type     Pri   Ver  Flags     Name";
  74. PRIVATE UBYTE *fmt = "%08LX %-8.8s %4d %3d   %08LX  %-32.32s";
  75.  
  76. PRIVATE ULONG Residents = 0L; // SysBase->ResModules.
  77.  
  78. // -------------------------------------------------------------------
  79.  
  80. #define NODELENGTH 80
  81.  
  82. PRIVATE struct List         SRList     = { 0, };
  83. PRIVATE struct Node        *SRNodes    = NULL;
  84. PRIVATE struct ListViewMem *Reslvm     = NULL; //{ 0, 0, 0, NODELENGTH };
  85.  
  86. PRIVATE UBYTE              *NodeStrs   = NULL;
  87.  
  88. // -------------------------------------------------------------------
  89.  
  90. PRIVATE UWORD SRGTypes[] = {
  91.  
  92.    LISTVIEW_KIND, BUTTON_KIND, BUTTON_KIND,
  93.    BUTTON_KIND,   BUTTON_KIND, BUTTON_KIND,
  94. };
  95.  
  96. PRIVATE int SLVClicked(       int whichitem );
  97. PRIVATE int UpdateClicked(    int dummy     );
  98. PRIVATE int CancelClicked(    int dummy     );
  99. PRIVATE int AddBtClicked(     int dummy     );
  100. PRIVATE int RemoveBtClicked(  int dummy     );
  101. PRIVATE int ReplaceBtClicked( int dummy     );
  102.  
  103. PRIVATE struct NewGadget SRNGad[] = {
  104.  
  105.      2,   3, 627, 200,                NULL, NULL, SLV,               0, 
  106.    NULL, (APTR) SLVClicked,
  107.  
  108.      4, 205,  72,  17, (UBYTE *) "_Update", NULL, Update, PLACETEXT_IN, 
  109.    NULL, (APTR) UpdateClicked,
  110.  
  111.     80, 205,  72,  17, (UBYTE *) "Add", NULL, AddBt, PLACETEXT_IN, 
  112.    NULL, (APTR) AddBtClicked,
  113.  
  114.    155, 205,  72,  17, (UBYTE *) "Remove", NULL, RemoveBt, PLACETEXT_IN, 
  115.    NULL, (APTR) RemoveBtClicked,
  116.  
  117.    230, 205,  72,  17, (UBYTE *) "Replace", NULL, ReplaceBt, PLACETEXT_IN, 
  118.    NULL, (APTR) ReplaceBtClicked,
  119.  
  120.    554, 205,  72,  17, (UBYTE *) "_Cancel", NULL, Cancel, PLACETEXT_IN, 
  121.    NULL, (APTR) CancelClicked
  122. };
  123.  
  124. PRIVATE ULONG SRGTags[] = {
  125.  
  126. //   GTLV_ReadOnly,     TRUE, 
  127.    GTLV_ShowSelected, NULL, GTLV_Selected, 1,
  128.    LAYOUTA_Spacing,   2, 
  129.    TAG_DONE,
  130.    
  131.    GT_Underscore, '_', TAG_DONE,
  132.    TAG_DONE,
  133.    TAG_DONE,
  134.    TAG_DONE,
  135.    GT_Underscore, '_', TAG_DONE
  136. };
  137.  
  138. // -------------------------------------------------------------------
  139.  
  140. PRIVATE void SetupLV( struct List *list, struct ListViewMem *lvm )
  141. {
  142.    int i, length = lvm->lvm_NodeLength;
  143.    
  144.    lvm->lvm_Nodes[0].ln_Succ = (struct Node *) list->lh_Tail;
  145.    lvm->lvm_Nodes[0].ln_Pred = (struct Node *) list->lh_Head;
  146.    lvm->lvm_Nodes[0].ln_Type = 0;
  147.    lvm->lvm_Nodes[0].ln_Pri  = lvm->lvm_NumItems - 129;
  148.    lvm->lvm_Nodes[0].ln_Name = ttl;
  149.  
  150.    for (i = 1; i <= lvm->lvm_NumItems; i++)
  151.       {
  152.       lvm->lvm_Nodes[i].ln_Name = &lvm->lvm_NodeStrs[ i * length ];
  153.       lvm->lvm_Nodes[i].ln_Pri  = lvm->lvm_NumItems - i - 129;
  154.       }
  155.  
  156.    NewList( (struct List *) list );      
  157.  
  158.    for (i = 0; i < lvm->lvm_NumItems; i++)
  159.       Enqueue( (struct List *) list, &lvm->lvm_Nodes[ i ] );
  160.  
  161.    return;
  162. }
  163.  
  164. PRIVATE int InitializeLV( int numitems )
  165. {
  166.    if ((Reslvm = Guarded_AllocLV( numitems, NODELENGTH )) == NULL)
  167.       {
  168.       ReportAllocLVError();
  169.  
  170.       return( -1 );
  171.       }
  172.  
  173.    NodeStrs = Reslvm->lvm_NodeStrs;
  174.    SRNodes  = Reslvm->lvm_Nodes;
  175.  
  176.    SetupLV( &SRList, Reslvm );
  177.  
  178.    return( 0 );
  179. }
  180.  
  181. PRIVATE int CountResidents( void )
  182. {
  183.    char **pointer = 0L;
  184.    int    rval    = 0;
  185.    
  186.    Forbid();
  187.    
  188.       if (Residents == NULL)
  189.          Residents = (ULONG *) SysBase->ResModules;
  190.       
  191.       pointer = (char **) Residents;
  192.  
  193.       while (*pointer != NULL) // pointer->rt_Name != NULL???? 
  194.          {
  195.          pointer++;
  196.          rval++;
  197.          }      
  198.    
  199.    Permit();
  200.    
  201.    return( rval + 2 ); // add some padding.
  202. }
  203.  
  204. PRIVATE char nt[10] = "", *nodeType = &nt[0];
  205.  
  206. PRIVATE char *GetType( UBYTE nodetype )
  207. {
  208.    switch (nodetype)
  209.       {
  210.       case NT_LIBRARY:
  211.          strcpy( nodeType, "Library" );
  212.          break;
  213.  
  214.       case NT_RESOURCE:
  215.          strcpy( nodeType, "Resource" );
  216.          break;
  217.  
  218.       case NT_DEVICE:
  219.          strcpy( nodeType, "Device" );
  220.          break;
  221.  
  222.       case NT_TASK:
  223.          strcpy( nodeType, "Task" );
  224.          break;
  225.  
  226.       default:
  227.          strcpy( nodeType, "Unknown" );
  228.          break;
  229.       }
  230.       
  231.    return( nodeType );
  232. }
  233.  
  234. PRIVATE void MakeResidentList( void )
  235. {
  236.    char **resident = 0L;
  237.    int    i;
  238.  
  239.    DisplayTitle( SRWnd, "Making list of Residents..." );
  240.    
  241.    HideListFromView( SLVGADGET, SRWnd );
  242.  
  243.    Forbid();
  244.       resident = (char **) SysBase->ResModules;
  245.       
  246.       for (i = 1; (*resident != NULL) && (i <= Reslvm->lvm_NumItems); i++)
  247.          {
  248.          //"Address  Type     Pri  Ver  Flags     Name"
  249.          sprintf( &NodeStrs[ i * NODELENGTH ], fmt,
  250.                   *resident, 
  251.  
  252.                   GetType( ((struct Resident *) *resident)->rt_Type ), 
  253.  
  254.                   ((struct Resident *) *resident)->rt_Pri,
  255.                   ((struct Resident *) *resident)->rt_Version,
  256.                   ((struct Resident *) *resident)->rt_Flags,
  257.                   ((struct Resident *) *resident)->rt_Name
  258.                 );
  259.       
  260.          resident++;
  261.          }
  262.  
  263.    Permit();
  264.  
  265.    ModifyListView( SLVGADGET, SRWnd, &SRList, NULL );
  266.  
  267.    DisplayTitle( SRWnd, SRWdt );
  268.  
  269.    return;
  270. }
  271.  
  272. // -------------------------------------------------------------------
  273.  
  274. PRIVATE BOOL  ResSelected  = FALSE;
  275. PRIVATE ULONG SelectedAddr = 0L;
  276.  
  277. PRIVATE int SLVClicked( int whichitem )
  278. {
  279.    if (whichitem == 0)
  280.       {
  281.       ResSelected = FALSE;
  282.       return( TRUE );  // Dopey User selected the column titles!
  283.       }
  284.    else
  285.       {
  286.       char t[256];
  287.       long addr = 0L;
  288.       
  289.       strcpy( t, &Reslvm->lvm_NodeStrs[ whichitem * NODELENGTH ] );
  290.       
  291.       (void) stch_l( t, &addr );
  292.       
  293.       sprintf( &t[0], "%s - you selected: 0x%08LX", SRWdt, addr );
  294.  
  295.       DisplayTitle( SRWnd, &t[0] );
  296.  
  297.       ResSelected  = TRUE;
  298.       SelectedAddr = (ULONG) addr;
  299.       }
  300.  
  301.    return( TRUE );
  302. }
  303.  
  304. PRIVATE void CloseSRWindow( void );
  305.  
  306. PRIVATE int UpdateClicked( int dummy )
  307. {
  308.    int count = CountResidents();
  309.    
  310.    if (count > Reslvm->lvm_NumItems)
  311.       {
  312.       // Deallocate old memory & reallocate for count variable:
  313.       Guarded_FreeLV( Reslvm );
  314.  
  315.       if (InitializeLV( count ) < 0)
  316.          {
  317.          sprintf( ErrMsg, "Couldn't get more memory for:\n"
  318.                           "   SysResidents Requester!" 
  319.                 );
  320.  
  321.          UserInfo( ErrMsg, "Allocation Problem:" );
  322.   
  323.          CloseSRWindow();
  324.  
  325.          return( FALSE );
  326.          }
  327.       }
  328.       
  329.    MakeResidentList();
  330.    
  331.    return( TRUE );
  332. }
  333.  
  334. PRIVATE int AddBtClicked( int dummy )
  335. {
  336.    // Show the user a string requester:   
  337.  
  338.    return( TRUE );
  339. }
  340.  
  341. PRIVATE int RemoveBtClicked( int dummy )
  342. {
  343.    if (ResSelected == FALSE)
  344.       {
  345.       strcpy( ErrMsg, "Select a Resident to remove first!" );
  346.  
  347.       UserInfo( ErrMsg, "User ERROR:" );
  348.       return( TRUE );
  349.       }
  350.    else
  351.       {
  352. //      char cmd[256];
  353. //      int  ans = 0;
  354.        
  355.       // Give the user a last chance to bail out:
  356.       
  357. //      ans = Handle_Problem();
  358.                    
  359. //      ((struct Resident *) SelectedAddr)->rt_Name
  360.       }
  361. }
  362.  
  363. PRIVATE int ReplaceBtClicked( int dummy )
  364. {
  365.    // Show the user a string requester:   
  366.    
  367.    return( TRUE );
  368. }
  369.  
  370. // -------------------------------------------------------------------
  371.  
  372. PRIVATE void CloseSRWindow( void )
  373. {
  374.    if (SRWnd != NULL)
  375.       {
  376.       CloseWindow( SRWnd );
  377.       SRWnd = NULL;
  378.       }
  379.  
  380.    if (SRGList != NULL)
  381.       {
  382.       FreeGadgets( SRGList );
  383.       SRGList = NULL;
  384.       }
  385.  
  386.    if (SRFont != NULL)
  387.       {
  388.       CloseFont( SRFont );
  389.       SRFont = NULL;
  390.       }
  391.  
  392.    return;
  393. }
  394.  
  395. PRIVATE int SRCloseWindow( void )
  396. {
  397.    CloseSRWindow();
  398.    return( FALSE );
  399. }
  400.  
  401. PRIVATE int CancelClicked( int dummy )
  402. {
  403.    return( SRCloseWindow() );
  404. }
  405.  
  406. // -------------------------------------------------------------------
  407.  
  408. PRIVATE int OpenSRWindow( void )
  409. {
  410.    struct NewGadget  ng;
  411.    struct Gadget    *g;
  412.    UWORD             lc, tc;
  413.    UWORD             wleft = SRLeft, wtop = SRTop, ww, wh;
  414.  
  415.    ComputeFont( Scr, Font, &CFont, SRWidth, SRHeight );
  416.  
  417.    ww = ComputeX( CFont.FontX, SRWidth );
  418.    wh = ComputeY( CFont.FontY, SRHeight );
  419.  
  420.    if ((wleft + ww + CFont.OffX + Scr->WBorRight) > Scr->Width)
  421.       wleft = Scr->Width - ww;
  422.  
  423.    if ((wtop + wh + CFont.OffY + Scr->WBorBottom) > Scr->Height)
  424.       wtop = Scr->Height - wh;
  425.  
  426.    if ((SRFont = OpenDiskFont( Font )) == NULL)
  427.       return( -5 );
  428.  
  429.    if ((g = CreateContext( &SRGList )) == NULL)
  430.       return( -1 );
  431.  
  432.    for (lc = 0, tc = 0; lc < SR_CNT; lc++)
  433.       {
  434.       CopyMem( (char *) &SRNGad[lc], (char *) &ng, 
  435.                (long) sizeof( struct NewGadget )
  436.              );
  437.  
  438.       ng.ng_VisualInfo = VisualInfo;
  439.       ng.ng_TextAttr   = Font;
  440.  
  441.       ng.ng_LeftEdge   = CFont.OffX + ComputeX( CFont.FontX, 
  442.                                                 ng.ng_LeftEdge
  443.                                               );
  444.  
  445.       ng.ng_TopEdge    = CFont.OffY + ComputeY( CFont.FontY,
  446.                                                 ng.ng_TopEdge
  447.                                               );
  448.  
  449.       ng.ng_Width      = ComputeX( CFont.FontX, ng.ng_Width );
  450.       ng.ng_Height     = ComputeY( CFont.FontY, ng.ng_Height );
  451.  
  452.       SRGadgets[lc] = g 
  453.                     = CreateGadgetA( (ULONG) SRGTypes[lc], 
  454.                                      g, 
  455.                                      &ng, 
  456.                                      (struct TagItem *) &SRGTags[tc]
  457.                                    );
  458.  
  459.       while (SRGTags[tc] != TAG_DONE)
  460.          tc += 2;
  461.  
  462.       tc++;
  463.  
  464.       if (g == NULL)
  465.          return( -2 );
  466.       }
  467.  
  468.    if ((SRWnd = OpenWindowTags( NULL,
  469.  
  470.                   WA_Left,    wleft,
  471.                   WA_Top,     wtop,
  472.                   WA_Width,   ww + CFont.OffX + Scr->WBorRight,
  473.                   WA_Height,  wh + CFont.OffY + Scr->WBorBottom,
  474.                   
  475.                   WA_IDCMP,   LISTVIEWIDCMP | BUTTONIDCMP 
  476.                     | IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW 
  477.                     | IDCMP_VANILLAKEY,
  478.                   
  479.                   WA_Flags,   WFLG_DRAGBAR | WFLG_DEPTHGADGET 
  480.                     | WFLG_CLOSEGADGET | WFLG_SMART_REFRESH 
  481.                     | WFLG_ACTIVATE | WFLG_RMBTRAP,
  482.  
  483.                   WA_Gadgets, SRGList,
  484.                   WA_Title,   SRWdt,
  485.                   TAG_DONE )
  486.       ) == NULL)
  487.       return( -4 );
  488.  
  489.    GT_RefreshWindow( SRWnd, NULL );
  490.  
  491.    return( 0 );
  492. }
  493.  
  494. PRIVATE int SRVanillaKey( int whichkey )
  495. {
  496.    int rval = TRUE;
  497.    
  498.    switch (whichkey)
  499.       {
  500.       case 'c':
  501.       case 'C':
  502.       case 'q':
  503.       case 'Q':
  504.       case 'x':
  505.       case 'X':
  506.          rval = CancelClicked( 0 );
  507.          break;
  508.          
  509.       case 'u':
  510.       case 'U':
  511.          rval = UpdateClicked( 0 );
  512.          break;
  513.       }
  514.       
  515.    return( rval );
  516. }
  517.  
  518. PRIVATE int HandleSRIDCMP( void )
  519. {
  520.    struct IntuiMessage  *m;
  521.    int                 (*func)( int code );
  522.    BOOL                  running = TRUE;
  523.  
  524.    while (running == TRUE)
  525.       {
  526.       if ((m = GT_GetIMsg( SRWnd->UserPort )) == NULL)
  527.          {
  528.          (void) Wait( 1L << SRWnd->UserPort->mp_SigBit );
  529.          continue;
  530.          }
  531.  
  532.       CopyMem( (char *) m, (char *) &SRMsg, 
  533.                (long) sizeof( struct IntuiMessage )
  534.              );
  535.  
  536.       GT_ReplyIMsg( m );
  537.  
  538.       switch (SRMsg.Class)
  539.          {
  540.          case IDCMP_REFRESHWINDOW:
  541.             GT_BeginRefresh( SRWnd );
  542.             GT_EndRefresh( SRWnd, TRUE );
  543.             break;
  544.  
  545.          case IDCMP_CLOSEWINDOW:
  546.             running = SRCloseWindow();
  547.             break;
  548.  
  549.          case IDCMP_VANILLAKEY:
  550.             running = SRVanillaKey( SRMsg.Code );
  551.             break;
  552.             
  553.          case IDCMP_GADGETUP:
  554.          case IDCMP_GADGETDOWN:
  555.             func = (void *) ((struct Gadget *) SRMsg.IAddress)->UserData;
  556.             
  557.             if (func != NULL)
  558.                running = func( SRMsg.Code );
  559.             break;
  560.          }
  561.       }
  562.  
  563.    return( running );
  564. }
  565.  
  566. PUBLIC int SystemResidents( void )
  567. {
  568.    int numitems = 0;
  569.  
  570.    if (SetupSystemList( &OpenSRWindow ) < 0)
  571.       {
  572.       fprintf( stderr, "Couldn't open a System ListViewer!\n" );
  573.       return( RETURN_FAIL );
  574.       }
  575.  
  576.    SetNotifyWindow( SRWnd );
  577.          
  578.    Forbid();
  579.       Residents = SysBase->ResModules;
  580.    Permit();
  581.  
  582.    numitems = CountResidents();
  583.  
  584.    if (InitializeLV( numitems ) < 0)
  585.       {
  586.       sprintf( ErrMsg, "Couldn't get memory for:\n"
  587.                        "   SysResidents Requester!" 
  588.              );
  589.  
  590.       UserInfo( ErrMsg, "Allocation Problem:" );
  591.  
  592.       CloseSRWindow();
  593.  
  594.       return( RETURN_FAIL );
  595.       }
  596.  
  597.    MakeResidentList();
  598.       
  599.    GT_RefreshWindow( SRWnd, NULL );
  600.  
  601.    (void) HandleSRIDCMP();
  602.  
  603.    Guarded_FreeLV( Reslvm );
  604.  
  605.    CloseSRWindow();         // Just in case.   
  606.  
  607.    ShutdownSystemList();
  608.       
  609.    return( RETURN_OK );
  610. }
  611.  
  612. #ifdef DEBUG
  613.  
  614. PUBLIC int main( void )
  615. {
  616.    return( SystemResidents() );
  617. }
  618.  
  619. #endif
  620.  
  621. /* ----------------- END of SysResident.c file! ------------------------ */
  622.